package de.tub.bazinga.object.particle;

import processing.core.PApplet;
import processing.core.PVector;
import de.tub.bazinga.object.Color;
import de.tub.bazinga.object.interf.Drawable;

public class Particle implements Drawable {

	private static final int PARTICLE_SIZE = 1;

	private PVector position;
	private PVector velocity;
	private PVector accleration;
	private int lifeTime;

	private final ParticleBehavior particleBehavior;

	private final Color particleColor;

	public Particle(PVector position, ParticleBehavior particleBehavior) {
		this.particleBehavior = particleBehavior;
		particleColor = this.particleBehavior.getColor();
		reset(position);
	}

	public void update() {
		lifeTime--;
		accleration = particleBehavior.getAccleration(position);
		velocity = particleBehavior.getVelocity(velocity, accleration);
		position = PVector.add(position, velocity);
	}

	public boolean isAlive() {
		return lifeTime > 0;
	}

	@Override
	public void draw(PApplet window) {
		window.stroke(particleColor.getValue());
		window.fill(particleColor.getValue());
		window.rect(position.x - PARTICLE_SIZE / 2.0f, position.y
				- PARTICLE_SIZE / 2.0f, PARTICLE_SIZE, PARTICLE_SIZE);
	}

	public void reset(PVector position) {
		this.position = position;
		this.velocity = particleBehavior.getStartVelocity();
		this.accleration = particleBehavior.getAccleration(position);
		this.lifeTime = particleBehavior.getLifeTime();
	}
}
